﻿/*                       NOTICE
 * 
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-10-C-0001, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * Approved for Public Release: 11-2435
 * © 2011 The MITRE Corporation. All Rights Reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Text;
using TUIO;

namespace GRIP.Gestures
{
    public abstract class ComplexGesture : Gesture
    {
        public ComplexGesture(DateTime Time, long ID, string Source)
            : base(Time, ID, Source)
        {
        }

        public List<BasicGesture> Children = new List<BasicGesture>();
        public abstract bool Update();
        public abstract bool ChildRemoved(BasicGesture g);
        public abstract void Destroy(DateTime time);
    }


    public abstract class ComplexGesture<T> : ComplexGesture where T : ComplexGesture<T>
    {
        public static event EventHandler<GestureEventArgs<T>> Added;
        public static event EventHandler<GestureEventArgs<T>> Changed;
        public static event EventHandler<GestureEventArgs<T>> Removed;
        public static List<T> ActiveGestures = new List<T>();

        public static void AddListeners(
            Action<object, GestureEventArgs<T>> OnAdded,
            Action<object, GestureEventArgs<T>> OnChanged,
            Action<object, GestureEventArgs<T>> OnRemoved)
        {
            Added += new EventHandler<GestureEventArgs<T>>(OnAdded);
            Changed += new EventHandler<GestureEventArgs<T>>(OnChanged);
            Removed += new EventHandler<GestureEventArgs<T>>(OnRemoved);
        }

        public ComplexGesture(DateTime Time, long ID, string Source, params BasicGesture[] children)
            : base(Time, ID, Source)
        {
            Children.AddRange(children);
            foreach (BasicGesture child in children)
                child.SetParent(this);
            Recognizer.PendingEvents.Add(new GestureEvent<Gesture>(this, GestureEventType.Added));
        }

        public override bool Update()
        {
            Recognizer.PendingEvents.Add(new GestureEvent<Gesture>(this, GestureEventType.Changed));
            return true;
        }

        public override void Destroy(DateTime time)
        {
            this.EndTime = time;
            foreach (BasicGesture child in Children)
                child.ParentDestroyed();
            Recognizer.PendingEvents.Add(new GestureEvent<Gesture>(this, GestureEventType.Removed));
        }

        public override void OnAdded(ContactFrameReadyEventArgs Frame)
        {
            T gesture = (T)this;
            ActiveGestures.Add(gesture);
            if (Added != null)
                Added(Frame.AdapterSource, new GestureEventArgs<T>(gesture, Frame.FrameNumber));
        }

        public override void OnChanged(ContactFrameReadyEventArgs Frame)
        {
            if (Changed != null)
                Changed(Frame.AdapterSource, new GestureEventArgs<T>((T)this, Frame.FrameNumber));
        }

        public override void OnRemoved(ContactFrameReadyEventArgs Frame)
        {
            T gesture = (T)this;
            ActiveGestures.Remove(gesture);
            if (Removed != null)
                Removed(Frame.AdapterSource, new GestureEventArgs<T>(gesture, Frame.FrameNumber));
        }
    }
}
